/***************************************************
 @Name :        小铭同学
 @Time :        2025-09-03
 @Email :       LiaoMingWJ233@163.com
 @Comment :     家电控制
 @FileName :    device_control.cpp
 @Version :     1.0
****************************************************/


#include "device_control.h"
#include "ui_device_control.h"

Device_Control::Device_Control(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Device_Control)
{
    ui->setupUi(this);
    
    beep_control = new beepdevice;
    fan_control = new FanDevice;
    moto_control = new VibratorDevice;
    moto_control_Thread = new VibratorThread(moto_control);
    
    Device_Control_initial(ui->pushButton_fan);
    Device_Control_initial(ui->pushButton_beep);
    Device_Control_initial(ui->pushButton_moto);
    
    connect(ui->pushButton_fan, &QPushButton::clicked, this, [=]() {        
        Fan_Control_state();
    });
    
    connect(ui->pushButton_beep, &QPushButton::clicked, this, [=]() {        
        Beep_Control_state();
    });
    
    connect(ui->pushButton_moto, &QPushButton::clicked, this, [=]() {        
        Moto_Control_state();
    });
    
    ui->label_zt->setText("家电控制");
    ui->label_zt->setStyleSheet("QLabel {"
        "    font-size: 50px;"           // 字体大小
        "    color: #87cefa;"            // 文字颜色
        "    font-weight: bold;"         // 字体粗细
        "    font-family: '楷体';"        // 字体家族
        "}"
    );
    
    ui->label_fan->setPixmap(QPixmap(":/风扇"));
    ui->label_beep->setPixmap(QPixmap(":/音响"));
    ui->label_moto->setPixmap(QPixmap(":/震动马达"));    
    
}

Device_Control::~Device_Control()
{
    delete ui;
}

void Device_Control::Device_Control_initial(QPushButton *Device)
{
    Device->setText("");
    Device->setFlat(true);
    Device->setIcon(QIcon(":/旋钮_关"));      
    Device->setIconSize(QSize(210, 210));
}

void Device_Control::Fan_Control_state()
{
    int value=0;
    char buf[4];
    
    // 循环切换状态：0→1→2→3→0...    
    fan_State = (fan_State + 1);
    if(fan_State>4) fan_State=1;
    
    // 根据状态设置不同的图标
    switch (fan_State) {
        case 1:  // 低速
            ui->pushButton_fan->setIcon(QIcon(":/旋钮_开1档"));
            value = 102;            
            snprintf(buf, sizeof(buf), "%d", value);
            fan_control->FanControl(buf);
            break;
        case 2:  // 中速
            ui->pushButton_fan->setIcon(QIcon(":/旋钮_开2档"));
            value = 140;            
            snprintf(buf, sizeof(buf), "%d", value);
            fan_control->FanControl(buf);
            break;
        case 3:  // 高速
            ui->pushButton_fan->setIcon(QIcon(":/旋钮_开3档"));
            value = 250;            
            snprintf(buf, sizeof(buf), "%d", value);
            fan_control->FanControl(buf);
            break;
        case 4:  // 关闭
            ui->pushButton_fan->setIcon(QIcon(":/旋钮_关"));
            fan_control->FanTurnOff();
            break;
    }
}

// 蜂鸣器控制函数实现
void Device_Control::Beep_Control_state()
{
    // 循环切换状态：0→1→2→3→0...    
    beep_State = (beep_State + 1);
    if(beep_State > 4) beep_State = 1;
    
    // 根据状态设置不同的图标
    switch (beep_State) {
        case 1:  // 低速
            ui->pushButton_beep->setIcon(QIcon(":/旋钮_开1档"));
            beep_control->setPwm(400);
            break;
        case 2:  // 中速
            ui->pushButton_beep->setIcon(QIcon(":/旋钮_开2档"));
            beep_control->setPwm(700);
            break;
        case 3:  // 高速
            ui->pushButton_beep->setIcon(QIcon(":/旋钮_开3档"));
            beep_control->setPwm(1000);
            break;
        case 4:  // 关闭
            ui->pushButton_beep->setIcon(QIcon(":/旋钮_关"));
            beep_control->beepOff();
            break;
    }
}

// 马达控制函数实现
void Device_Control::Moto_Control_state()
{
    // 循环切换状态：0→1→2→3→0...    
    moto_State = (moto_State + 1);
    if(moto_State > 4) moto_State = 1;
    
    // 根据状态设置不同的图标
    switch (moto_State) {
        case 1:  // 低速
            ui->pushButton_moto->setIcon(QIcon(":/旋钮_开1档"));
            frequency = STRONGMAGNITUDE_2;
            moto_control_Thread->frequency = frequency;
            moto_control_Thread->start(); 
            break;
        case 2:  // 中速
            ui->pushButton_moto->setIcon(QIcon(":/旋钮_开2档"));
            frequency = STRONGMAGNITUDE_3;
            moto_control_Thread->frequency = frequency;
            moto_control_Thread->start(); 
            break;
        case 3:  // 高速
            ui->pushButton_moto->setIcon(QIcon(":/旋钮_开3档"));
            frequency = STRONGMAGNITUDE_4;
            moto_control_Thread->frequency = frequency;
            moto_control_Thread->start(); 
            break;
        case 4:  // 关闭           
            ui->pushButton_moto->setIcon(QIcon(":/旋钮_关"));
            frequency = STRONGMAGNITUDE_0;
            moto_control_Thread->frequency = frequency;
            moto_control_Thread->start(); 
            break;
    }
}
